package org.iridium.xl.order;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.iridium.xl.order.vo.ProductItem;
import org.iridium.xl.order.vo.VstOrder;

import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.iridium.xl.order.util.DateTools;

/**
 *
 * @author iridiumcao
 */
public class PrintTools {

    public void genPrints(String excelPath, String template, String destDir) {
        Configuration cfg = new Configuration();
        try {
            File tempFile = new File(template);

            cfg.setDirectoryForTemplateLoading(tempFile.getParentFile());
            cfg.setObjectWrapper(new DefaultObjectWrapper());
            cfg.setDefaultEncoding("UTF-8");

            Template temp = cfg.getTemplate(tempFile.getName());
            temp.setEncoding("UTF-8");

            DataParser printTool = new DataParser();
            List<VstOrder> orders = printTool.buildOrderList(excelPath);

            for (VstOrder order : orders) {

                Map<String, Object> root = new HashMap<String, Object>();
                root.put("userName", order.getUserName());// 用户名
                root.put("customNo", order.getCustomNo());// 客户编号
                root.put("orderNo", order.getOrderNo());// 订单编号
                root.put("currentDate", DateTools.getStringFromDate(new java.util.Date()));//
                root.put("orderDate", DateTools.getStringFromDate(order.getOrderDate()));// 下单时间
                root.put("acceptor", order.getAcceptor());// 客户编号
                root.put("address", order.getAddress());// 客户编号
                root.put("tel", order.getTel());// 联系电话


                List products = new ArrayList();
                root.put("products", products);
                double totalAmount = 0.0;
                for (ProductItem product : order.getProducts()) {
                    totalAmount += product.getPrice();
                    Map<String, Object> p = new HashMap<String, Object>();
                    p.put("productCode", product.getProductCode());// 商品代码
                    p.put("productName", product.getProductName());// 商品名称
                    p.put("clazz", product.getClazz());//规格
                    p.put("model", product.getModel());// 单位
                    p.put("count", product.getCount());// 数量
                    p.put("price", product.getPrice());// 单价
                    p.put("amount", product.getAmount());// 金额

                    products.add(p);
                }

                root.put("totalAmount", totalAmount); //合计金额

                File file = new File(new File(destDir), order.getOrderNo() + order.getAcceptor()
                        + ".html");
                file.createNewFile();

                Writer out = new BufferedWriter(new OutputStreamWriter(
                        new FileOutputStream(file), "UTF-8"));
                temp.process(root, out);
                out.flush();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TemplateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}